package com.beiding.jsonanalyzer.analyze.func.openfunc;

import com.beiding.jsonanalyzer.analyze.func.CollectionFunc;
import com.beiding.jsonanalyzer.analyze.func.base.CollectionFuncAdaptor;
import com.beiding.jsonanalyzer.analyze.func.base.EasyFunc;
import com.beiding.jsonanalyzer.utils.TextUtils;

import java.util.Arrays;

/**
 * 日期格式化
 *
 * @author 丁常磊
 * @date 2022/5/19 11:23
 */

public class Split {


    public static CollectionFunc of(Object key, String by) {
        return of(key, by, 0);
    }

    public static CollectionFunc of(Object key, String by, int limit) {
        return FuncUtils.buildForEasyFunc(new CollectionFuncAdaptor<EasyFunc>() {
            @Override
            protected EasyFunc newInstance() {
                return new EasyFunc() {
                    @Override
                    protected Object execute(Object o) {
                        if (o instanceof String) {
                            return Arrays.asList(((String) o).split(by,limit));
                        }
                        return null;
                    }
                    @Override
                    public String toString() {
                        return "split(" + TextUtils.addFormat(core,"\t") + ",'" + by+"'" + (limit > 0 ? "," + limit : "") + ")";
                    }
                };

            }
        }, key);
    }

}
